﻿Public Class CsvWriter

    Private _headerData As List(Of String)
    Private _data As List(Of List(Of String))

    Public Sub New(ByVal data As List(Of List(Of String)), ByVal headerData As List(Of String))

        _headerData = headerData
        _data = data

    End Sub

    Public Sub Write(ByVal path As String)

        If Not IO.File.Exists(path) OrElse
            MessageBox.Show(String.Format("Overwrite {0}?", path), "BattleAxePursuit", MessageBoxButtons.YesNo) = DialogResult.Yes Then

            Dim writer As New IO.StreamWriter(path)

            Try
                
                Dim lineToWrite As String = String.Empty

                'compile first line
                For Each datum As String In _headerData
                    lineToWrite += datum + ","
                Next
                lineToWrite = lineToWrite.Substring(0, lineToWrite.Length - 1)
                writer.WriteLine(lineToWrite)

                'compile each data line
                For Each line As List(Of String) In _data

                    lineToWrite = String.Empty

                    For Each datum As String In line
                        lineToWrite += datum + ","
                    Next

                    lineToWrite = lineToWrite.Substring(0, lineToWrite.Length - 1)
                    writer.WriteLine(lineToWrite)

                Next

            Catch ex As Exception
                MessageBox.Show("Error in CsvWriter::Write(path): " + ex.Message, "BattleAxePursuit", MessageBoxButtons.OK)
            Finally
                writer.Close()
            End Try
        End If

    End Sub

End Class
